package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminWarnDao;
import ex.com.jm.food.entity.Warnnings;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminWarnDaoImpl extends HibernateDaoSupport implements
		AdminWarnDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Warnnings> getWarnnings(final int initPage,final int pageRow,final String name,
			final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		Object warns=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql=" SELECT "
						+ " warn.warn_id,"
						+ " warn.warn_count,"
						+ " warn.com_id,"
						+ " warn.warn_why,"
						+ " warn.created, "
						+ " com.com_name  "
						+ " FROM jm_warn AS  warn "
						+ " LEFT JOIN jm_company AS com ON com.com_id=warn.com_id "
						+ "WHERE 1=1 ";
				if(name!=null&&!name.equals("")){
					sql+=" and com.com_name like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					sql+=" and warn.created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					sql+=" and warn.created <='"+e_date+"'";
				}
				sql +=" ORDER BY warn.created desc ";
				Query query=session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return  (List<Warnnings>) warns;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int warnCount(final String name,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql=" SELECT "
						+ " warn.warn_id,"
						+ " warn.warn_count,"
						+ " warn.com_id,"
						+ " warn.warn_why,"
						+ " warn.created, "
						+ " com.com_name  "
						+ " FROM jm_warn AS  warn "
						+ " LEFT JOIN jm_company AS com ON com.com_id=warn.com_id "
						+ "WHERE 1=1 ";
				if(name!=null&&!name.equals("")){
					sql+=" and com.com_name like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					sql+=" and warn.created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					sql+=" and warn.created <='"+e_date+"'";
				}
				Query query=session.createSQLQuery(sql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveWarn(Warnnings warnnings) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(warnnings);
	}

	@Override
	public void updateWarn(Warnnings warnnings) {
		// TODO Auto-generated method stub
          this.getHibernateTemplate().update(warnnings);
	}

	@Override
	public void delWarn(int id) {
		// TODO Auto-generated method stub
       this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Warnnings.class, id));
	}

	@Override
	public Warnnings editWarn(int id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Warnnings.class, id);
	}

}
